// pages/detail/detail.js
const time = require("../../utils/util.js"); //引入时间转换
const app = getApp()
Page({
  data: {
    luckyaddress: null, //中奖地址
    acid: '', //活动id
    host: '', //ip地址
    title: ["进行中", "已结束"], //活动状态
    //活动信息
    activityDetail: {}, //详细信息
    actionSheetHidden: true,
    detail: '',
    userInfo: null
  },
  /**
   * 显示分享图片画布函数
   */
  show: function() {
    wx.navigateTo({
      url: '../sharePicture/sharePicture',
    })
  },
  /**
   * 分享好友函数
   */
  shareToFriend: function(e) {
    var that = this
    that.onShareAppMessage()
  },
  /**
   * 跳转到发起抽奖页面
   */
  newOne: function(e) {
    wx.switchTab({
      url: '../newLottery/newLottery',
    })
  },
  /**
   * 监控分享弹出框显示函数
   */
  listenerActionSheet: function() {
    var self = this;
    self.setData({
      actionSheetHidden: !self.data.actionSheetHidden
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var acid = options.acid
    this.setData({
      acid: acid,
      host: app.globalData.ip
    })
    that.getActivityDetail()
    console.log("活动id：", that.data.acid)
  },
  /**
   * 获取活动详情，及用户与该活动关系
   */
  getActivityDetail: function() {
    var that = this
    //获取活动详情
    wx.request({
      url: that.data.host + '/jeesite/lottery/activity',
      method: "POST",
      header: {
        "Content-Type": "application/json"
      },
      data: {
        "message": "getActivityById",
        "activity_id": that.data.acid,
        "openid": app.globalData.openid
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 0) {
          var data = res.data.data
          var currenttime = data.end_time
          data.end_time = time.formatTime(currenttime, 'Y-M-D h:m:s')
          that.setData({
            activityDetail: data
          })
          console.log("获取列表成功", that.data.activityDetail)
          var detail = that.escape2Html(that.data.activityDetail.detail)
          that.setData({
            detail: detail
          })
        } else {
          console.log("获取失败 ", res)
        }
      },
      fail: function(res) {
        console.log("获取失败 ", res)
      }
    })
    // 隐藏导航栏加载框
    wx.hideNavigationBarLoading();
    // 停止下拉动作
    wx.stopPullDownRefresh();
  },
  /**
   * 返回首页
   */
  returnIndex: function(e) {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 点击抽奖按钮，参与抽奖
   */
  attend: function(e) {
    var that = this
    console.log(that.data.activityDetail.activity_id)
    if (app.globalData.openid != "") {
      wx.request({
        url: that.data.host + '/jeesite/lottery/activity',
        method: 'POST',
        header: {
          "Content-Type": "application/json"
        },
        data: {
          "message": "participate",
          "openid": app.globalData.openid,
          "activity_id": that.data.activityDetail.activity_id
        },
        success: function(res) {
          if (res.data.code == 0) {
            console.log("参与成功", res)
            wx.showToast({
              title: '参与成功！',
              icon: 'success',
              duration: 1000
            });
            that.getActivityDetail()
          } else {
            console.log("参与失败", res)
          }
        },
        fail: function(res) {
          console.log("参与请求失败", res.data.message)
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '用户未授权',
      })
    }
  },
  /**
   * 填写收货地址
   */
  setAddress: function(e) {
    var that = this
    // 跳转到地址列表页面，并选择地址初始化luckyaddress
    var flag = -1
    wx.navigateTo({
      url: '../addressList/addressList?flag=' + flag + '&acid=' + that.data.acid,
    })
  },
  /**
   * 发起人确认开奖
   */
  endActivity: function() {
    var that = his
    wx.request({
      url: that.data.host + '/jessite/lottery/activity',
      method: 'POST',
      data: {
        "message": 'run',
        "openid": app.globalData.openid,
        "activity_id": that.data.activityDetail.activity_id
      },
      success: function(res) {
        console.log(res.data)
        if (res.data.code == 0) {
          wx.showToast({
            title: '开奖成功！',
            icon: 'success',
            duration: 1000
          });
          that.getActivityDetail()
        } else {
          wx.showToast({
            title: '开奖失败！',
            icon: 'error',
            duration: 1000
          });
        }
      },
      fail: function(res) {
        console.log(res.data.message)
        wx.showModal({
          title: '提示',
          content: '开奖失败',
        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '分享给好友'
    }
  },
  /**
   * 富文本显示
   */
  escape2Html: function(str) {
    var arrEntities = {
      'lt': '<',
      'gt': '>',
      'nbsp': ' ',
      'amp': '&',
      'quot': '"',
      'middot': '·',
      'amp': '&',
      'brvbar': '¦',
      'mdash': '—',
      'ndash': '–',
      'ge': '≥',
      'le': '≤',
      'laquo': '«',
      'raquo': '»',
      'deg': '°',
      'bull': '•',
      'macr': '¯',
      '#64': '@'
    };
    return str.replace(/&(lt|gt|nbsp|amp|quot|middot|amp|brvbar|mdash|ndash|ge|le|laquo|raquo|deg|bull|macr|#64);/ig, function(all, t) {
      return arrEntities[t];
    });
  },
  //下拉刷新
  onPullDownRefresh: function(e) {
    var that = this
    wx.showNavigationBarLoading();
    that.getActivityDetail();
  }
})